package database;

import com.mysql.jdbc.Connection;
import entity.Backup;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author user192
 */
public class Mysql {

    private static final String url = "jdbc:mysql://10.28.10.194:3306/backup?useUnicode=true&characterEncoding=UTF-8";
    private static final String user = "root";
    private static final String password = "m1ngt14n";

    public static List<Backup> getBackups() {
        List<Backup> backups = new ArrayList<Backup>();
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = (Connection) DriverManager.getConnection(url, user, password);
            statement = connection.createStatement();
            String sql = "select * from backup";
            rs = statement.executeQuery(sql);

            while (rs.next()) {
                backups.add(new Backup(rs.getString("database_name"),
                        rs.getString("backup_time"),
                        rs.getString("path"),
                        rs.getString("host"),
                        rs.getString("user_name"),
                        rs.getString("password")));
            }

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Mysql.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Mysql.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (rs != null || !rs.isClosed()) {
                    rs.close();
                }
                if (!statement.isClosed()) {
                    statement.isClosed();
                }
                if (!connection.isClosed()) {
                    connection.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(Mysql.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return backups;
    }

    public static void main(String[] args) {
        for (Backup b : getBackups()) {
            System.out.print(b.getUserName() + "  ==  ");
            System.out.print(b.getPassword() + "  ==  ");
            System.out.print(b.getHost() + "  ==  ");
            System.out.print(b.getPath() + "  ==  ");
            System.out.print(b.getDatabaseName() + "  ==  ");
            System.out.print(b.getBackupTime() + "  ==  ");
            System.out.println();
        }
    }
}
